package com.ruoyi.system.controller.financial;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.common.BillTypeConstant;
import com.ruoyi.system.domain.db.financial.FinancialEconomic;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.MyPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * TODO : 还可以完成查询剩下的钱需要注意部分的钱是减掉的，不是一直加的
 */

@RestController
@RequestMapping("/financial/bill")
public class FinancialEconomicController extends BaseController {
    @Autowired
    private IFinancialEconomicService financialEconomicService;

    @Autowired
    private IExternalCooperationService externalCooperationService;

    @Autowired
    private ITradeCargoOrderService tradeCargoOrderService;

    @Autowired
    private ITransportTaskService transportTaskService;

    @Autowired
    private ITradeMaterialOrderService tradeMaterialOrderService;

    @Autowired
    private ITradeVehicleOrderService tradeVehicleOrderService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:list')")
    @GetMapping("/list")
    public PageInfo<FinancialEconomicVo> list(FinancialEconomic financialEconomic){
        //startPage();
        List<FinancialEconomicVo> list = financialEconomicService.selectFinancialEconomicList(financialEconomic);
        return MyPageUtils.startPage(list);
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:query')")
    @GetMapping("")
    public AjaxResult getInfo(@RequestParam(required = false) Long orderId)
    {
        return success(financialEconomicService.selectFinancialEconomicByOrderId(orderId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:add')")
    @Log(title = "账单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinancialEconomic financialEconomic)
    {
        return toAjax(financialEconomicService.insertFinancialEconomic(financialEconomic));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:edit')")
    @Log(title = "账单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinancialEconomic financialEconomic)
    {
        return toAjax(financialEconomicService.updateFinancialEconomic(financialEconomic));
    }

    /**
     * 删除
     * TODO : 不建议使用删除功能
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:remove')")
    @Log(title = "账单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("")
    public AjaxResult remove(@RequestBody List<Long> orderIds)
    {
        return toAjax(financialEconomicService.deleteFinancialEconomicByOrderIds(orderIds));
    }

    /**
     * 删除
     * TODO : 不建议使用删除功能
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:removeByid')")
    @Log(title = "账单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderId}")
    public AjaxResult removeById(@PathVariable Long orderId)
    {
        return toAjax(financialEconomicService.deleteFinancialEconomicByOrderId(orderId));
    }

    /**
     * 获取详细信息
     * @param billId
     * @return
     * 获取的数据只有一条，不需要分页吧
     * TODO : 还有很多的账单类型在这里加:case...
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:getDetailInfo')")
    @GetMapping("/getDetailInfo")
    public AjaxResult getDetailBill(@RequestParam Long billId){
        FinancialEconomicVo financialEconomicVo = financialEconomicService.selectFinancialEconomicByOrderId(billId);
        Long billDetailId = financialEconomicVo.getBillDetailId();
        Integer billTypeId = financialEconomicVo.getBillTypeId();
        switch (billTypeId) {
            case BillTypeConstant.COOPERATION_ADVERTISEMENT: // 合作商广告
                return success(externalCooperationService.selectExternalCooperationOrderByOrderId(billDetailId));
            case BillTypeConstant.CARGO_ORDER: // 顾客订单
                return success(tradeCargoOrderService.selectCargoOrderByOrderId(billDetailId));
            case BillTypeConstant.SALARY: // 工资
                return success("工资");
            case BillTypeConstant.COOPERATION_TRANSPORT: // 承运商运输
                return success(transportTaskService.selectTransportTaskByTaskId(billDetailId));
            case BillTypeConstant.MATERIAL_ORDER: // 购买材料
                return success(tradeMaterialOrderService.selectTradeMaterialOrderByOrderId(billDetailId));
            case BillTypeConstant.VEHICLE_ORDER: // 购买车辆
                return success(tradeVehicleOrderService.selectTradeVehicleOrderByOrderId(billDetailId));
            case BillTypeConstant.WAREHOUSE_SALARY: // 仓库租金
                return success("仓库租金");
            default:
                break;
        }
        return success();
    }

    /**
     * 获取该年所有月收入支出总数据
     *  分收入和支出
     *  输出总开销和账单条数
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:getMonthInfo')")
    @GetMapping("/getMonthInfo")
    public AjaxResult getMonthInfo(@RequestParam Date date){
        Map<String, List<MonthInfo>> monthInfo = financialEconomicService.getMonthInfo(date);
        return success(monthInfo);
    }

    /**
     * 获取该年该月按类型分类账单
     *  分收入和支出
     *  输出总开销和账单条数
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:getMonthTypeInfo')")
    @GetMapping("/getMonthTypeInfo")
    public AjaxResult getMonthTypeInfo(@RequestParam Date date){
        Map<String, List<MonthTypeInfo>> monthTypeInfo = financialEconomicService.getMonthTypeInfo(date);
        return success(monthTypeInfo);
    }

    /**
     * 获取某日的收入支出总数和账单条数
     */
    @PreAuthorize("@ss.hasPermi('financial:bill:getDateInfo')")
    @GetMapping("/getDateInfo")
    public AjaxResult getDateInfo(Date date){
        DateInfo dateInfo = financialEconomicService.getDateInfo(date);
        return success(dateInfo);
    }
}
